<template>
  <div>
    <my-breadcrumb :items="['社区管理', 'repo管理']"/>
    <search :formItems="searchItems" :check="true" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">添加Repo</el-button>
      <el-button type="primary" @click="toExport" :loading="loading">导出Repo</el-button>
    </div>
    <el-table :data="repos" border @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="ID"
        prop="id">
      </el-table-column>
      <el-table-column
        label="创建时间"
        sortable="custom"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="评价用户"
        :show-overflow-tooltip="true"
        prop="membername">
      </el-table-column>
      <el-table-column
        label="标题"
        :show-overflow-tooltip="true"
        prop="title">
      </el-table-column>
      <el-table-column
        label="裙子名称"
        prop="skirtname">
      </el-table-column>
      <el-table-column
        label="店铺名称"
        :show-overflow-tooltip="true"
        prop="businessname">
      </el-table-column>
      <el-table-column
        label="详情内容"
        prop="content">
      </el-table-column>
      <el-table-column
        label="点击量"
        sortable="custom"
        width="90px"
        prop="clickcount">
      </el-table-column>
      <el-table-column
        label="点赞量"
        sortable="custom"
        width="90px"
        prop="fabulous">
      </el-table-column>
      <el-table-column
        label="是否精选"
        width="90px"
        prop="isessence">
          <template slot-scope="scope">
            <span v-if="scope.row.isessence === '0'">否</span>
            <span v-if="scope.row.isessence === '1'">是</span>
          </template>
      </el-table-column>
      <el-table-column
        label="删除内容"
        prop="delcontent">
      </el-table-column>
      <el-table-column
        label="操作"
        width="180px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)" :disabled="scope.row.delflag === '1'">编辑</el-button>
          <el-button v-if="scope.row.delflag === '0'" size="mini" type="text" @click="toDelete(scope.row.id, scope.row.memberid)">删除</el-button>
          <el-button v-else size="mini" type="text" disabled>已删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '新增记录' : '信息修改'" :visible.sync="showAdd" width="500px" top="20px">
      <el-form :model="repo" :rules="rules" label-width="90px" ref="addForm">
        <el-form-item label="评价用户" prop="memberid">
          <div class="select-user">
            <img :src="nowUser.avatar" alt="" width="40" v-show="nowUser.avatar">
            <span v-show="nowUser.name" style="margin: 0 20px 0 12px;">{{ nowUser.name }}</span>
            <el-button type="text" @click="toSelectUser">{{ nowUser.name ? '重新选择用户' : '选择用户' }}</el-button>
          </div>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="repo.title" type="textarea"/>
        </el-form-item>
        <el-form-item label="裙子名称" prop="skirtname">
          <div class="select-user">
            <img :src="nowSkirt.img1" alt="" width="40" v-show="nowSkirt.img1">
            <span v-show="nowSkirt.name" style="margin: 0 20px 0 12px;">{{ nowSkirt.name }}</span>
            <el-button type="text" @click="toSelectSkirt">{{ nowSkirt.name ? '重新选择裙子' : '选择裙子' }}</el-button>
          </div>
        </el-form-item>
        <el-form-item label="商家名称" prop="businessname">
          <el-input v-model="repo.businessname" disabled/>
        </el-form-item>
        <el-form-item label="详情内容" prop="content">
          <el-input v-model="repo.content" type="textarea"/>
        </el-form-item>
        <el-form-item label="是否精选" prop="isessence">
          <el-radio-group v-model="repo.isessence">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="点击量" prop="clickcount">
          <el-input-number v-model="repo.clickcount"/>
        </el-form-item>
        <el-form-item label="点赞量" prop="fabulous">
          <el-input-number v-model="repo.fabulous"/>
        </el-form-item>
        <el-form-item label="性价比" prop="xjb">
          <el-rate v-model="repo.xjb"/>
        </el-form-item>
        <el-form-item label="店家服务" prop="djfw">
          <el-rate v-model="repo.djfw"/>
        </el-form-item>
        <el-form-item label="衣服质量" prop="yfzl">
          <el-rate v-model="repo.yfzl"/>
        </el-form-item>
        <el-form-item label="图片" prop="imgarr">
          <div class="img-box" v-for="(image, index) in imageArr" :key="index">
            <img :src="image" class="avatar">
            <i class="el-icon-close" @click="delImage(index)"></i>
          </div>
          <el-upload
            class="avatar-uploader"
            action="#"
            v-if="imageArr.length < 6"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
    <select-list ref="selectListUser" :url="'/Member/index?delflag=0'" :searchItems="userSearch" :tableItem="userTable" @selectRow="selectUser"/>
    <select-list ref="selectList" :url="'/Skirti/index?isbus=1&delflag=0'" :searchItems="skirtSearch" :tableItem="skirtTableItems" @selectRow="skirtChange"/>
  </div>
</template>

<script>
import {deletePrompt, getAllUser, toExport} from '@/utils/getList'
import selectList from '@/components/common/selectList'
import tableItems from '@/config/tableItems'
import CLAZZ from '@/config/classify'
export default {
  name: 'repo',
  data () {
    return {
      searchItems: [
        {
          label: '评价用户',
          type: 'input',
          key: 'membername'
        },
        {
          label: '标题',
          type: 'input',
          key: 'title'
        },
        {
          label: '裙子名称',
          type: 'input',
          key: 'skirtname'
        },
        {
          label: '店铺名称',
          type: 'input',
          key: 'businessname'
        },
        {
          label: '是否精选',
          type: 'select',
          key: 'isessence',
          options: [{id: '0', name: '否'}, {id: '1', name: '是'}]
        }
      ], // 检索结构
      userSearch: [
        {
          label: '用户名称',
          type: 'input',
          key: 'membername'
        },
        {
          label: '手机号',
          type: 'input',
          key: 'phone'
        },
        {
          label: '性别',
          type: 'select',
          key: 'sex',
          options: [{id: '1', name: '男'}, {id: '2', name: '女'}, {id: '3', name: '未知'}, {id: '4', name: '保密'}]
        },
        {
          label: '风格',
          type: 'select',
          key: 'styleid',
          options: CLAZZ.style
        },
        {
          label: '爱好程度',
          type: 'select',
          key: 'degreeid',
          options: CLAZZ.degreeid
        },
        {
          label: '地区',
          type: 'address',
          key: 'address'
        },
        {
          label: '拥有裙子数',
          type: 'input',
          key: 'skirtnum'
        },
        {
          label: '等级',
          type: 'select',
          key: 'level',
          options: [{id: '1', name: '一级'}, {id: '2', name: '二级'}, {id: '3', name: '三级'}]
        }
      ],
      skirtSearch: [
        {
          label: '裙子名称',
          type: 'input',
          key: 'skirtname'
        },
        {
          label: '商家名称',
          type: 'input',
          key: 'businessname'
        }
      ], // 检索结构
      repos: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      repo: {
        memberid: '',
        title: '',
        skirtname: '',
        skirtid: '',
        businessname: '',
        businessid: '',
        content: '',
        clickcount: 0,
        fabulous: 0,
        xjb: 0,
        djfw: 0,
        yfzl: 0,
        imgarr: [],
        isessence: ''
      },
      imageArr: [],
      rules: {
        memberid: [
          { required: true, message: '请选择用户', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入repo标题', trigger: 'blur' }
        ],
        skirtname: [
          { required: true, message: '请选择裙子', trigger: 'blur' }
        ],
        businessname: [
          { required: true, message: '请选择商家', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        clickcount: [
          { required: true, message: '请输入点击量', trigger: 'change' }
        ],
        imgarr: [
          { required: true, message: '请选择图片', trigger: 'change' }
        ],
        isessence: [
          { required: true, message: '请选择是否精选', trigger: 'change' }
        ],
        xjb: [
          { required: true, message: '请选择性价比', trigger: 'change' }
        ],
        djfw: [
          { required: true, message: '请选择店家服务', trigger: 'change' }
        ],
        yfzl: [
          { required: true, message: '请选择衣服质量', trigger: 'change' }
        ]
      },
      isAdd: true,
      showAdd: false,
      updateRow: null,
      users: [],
      loading: false,
      lifting: null,
      userTable: tableItems.userList,
      skirtTableItems: tableItems.skirtList, // 选择裙子表格结构
      nowUser: {},
      nowSkirt: {}
    }
  },
  methods: {
    getList () {
      let searchData = {delflag: '0'}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/repo/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            res.data.data.list.forEach(item => {
              item.djfw = Number(item.djfw)
              item.yfzl = Number(item.yfzl)
              item.xjb = Number(item.xjb)
              item.imgarr = [item.img1, item.img2, item.img3, item.img4, item.img5, item.img6]
            })
            this.repos = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.nowUser = {}
      this.nowSkirt = {}
      this.imageArr = []
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
        this.$refs['addForm'].clearValidate()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      let temp = []
      row.imgarr.forEach(item => {
        if (item) {
          temp.push(item)
        }
      })
      row.imgarr = temp
      this.$nextTick(() => {
        Object.keys(this.repo).forEach(key => {
          this.repo[key] = row[key]
        })
        this.nowUser = {name: row.membername}
        this.nowSkirt = {name: row.skirtname}
        this.imageArr = JSON.parse(JSON.stringify(row.imgarr))
        this.$refs['addForm'].clearValidate()
      })
    },
    toSubmit () {
      let url = '/repo/add'
      let params = {}
      Object.keys(this.repo).forEach(key => {
        params[key] = this.repo[key]
      })
      if (!this.isAdd) {
        url = '/repo/edit'
        params.id = this.updateRow.id
      }
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post(url, this.qs.stringify(params))
            .then(res => {
              console.log(res)
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.showAdd = false
                this.getList()
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          return false
        }
      })
    },
    toDelete (id, memberId) {
      deletePrompt({url: '/repo/delete', id, memberId}, (res) => {
        if (res.data.status === 1) {
          this.$message.success(res.data.msg)
          this.getList()
        }
      })
    },

    toExport () {
      this.loading = true
      let searchData = this.$refs['search'].search()
      toExport('/Repo/excelindex', searchData, 'repo', () => {
        this.loading = false
      })
    },

    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    },

    toSelectUser () {
      this.$refs['selectListUser'].toShow()
    },
    selectUser (row) {
      this.nowUser = row
      this.repo.memberid = row.id
    },

    toSelectSkirt () {
      this.$refs['selectList'].toShow()
    },
    skirtChange (row) {
      this.nowSkirt = row
      this.repo.skirtid = row.id
      this.repo.skirtname = row.skirtname
      this.repo.businessid = row.memberid
      this.repo.businessname = row.membername
    },

    beforeUpload (file) {
      let reader = new FileReader()
      let _self = this
      reader.onload = function (ev) {
        _self.imageArr.push(ev.currentTarget.result)
        _self.repo.imgarr.push(ev.currentTarget.result)
      }
      reader.readAsDataURL(file)
      return false
    },
    delImage (index) {
      this.imageArr.splice(index, 1)
      this.repo.imgarr.splice(index, 1)
    }
  },
  components: {
    selectList
  },
  created () {
    this.getList()
    getAllUser((data) => {
      this.users = data
    })
  }
}
</script>

<style scoped>
  .select-user{
    display: flex;
    align-items: center;
  }
  .avatar-uploader{
    display: inline-block;
    overflow: hidden;
  }
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .img-box{
    display: inline-block;
    position: relative;
  }
  .img-box i{
    position: absolute;
    top: 10px;
    right: 10px;
  }
  .avatar {
    width: 100px;
    height: 100px;
    margin-right: 10px;
    border-radius: 6px;
    overflow: hidden;
  }
</style>
